package br.com.twit4bus.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;

import br.com.twit4bus.conf.AccessTokenBuilder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.BoxLayout;

public class MenuConfAccessToken {

	private JFrame frmConfiguraAcessoA;
	private JTextField txtConKey;
	private JTextField txtConKeySecret;
	private JTextField txtAccessToken;
	private JTextField txtAccessTokenSecret;
	private JTextField txtReqUrl;
	private JTextField txtTokenUrl;
	private JTextField txtAuthUrl;
	private JLabel lblSituacao;

	private AccessTokenBuilder tokenBuilder;
	private JTextField txtPin;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MenuConfAccessToken window = new MenuConfAccessToken();
					window.frmConfiguraAcessoA.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MenuConfAccessToken() {
		initialize( );
		CarregaProperties( );
	}

	void CarregaProperties( ) {		
		tokenBuilder = new AccessTokenBuilder( ); 	
		LoadVarToText( );
		BloqueiaJTextFields( );
		AtualizaStatus( );
	}

	void LoadVarToText( ) {
		String consumerKey = "";
		String consumerKeySecret = "";
		String accessToken = "";
		String accessTokenSecret = "";
		String urlValidation = "";
		String pin = "";
		if( null != tokenBuilder ){
			consumerKey = tokenBuilder.getConsumerKey( );
			consumerKeySecret = tokenBuilder.getConsumerSecret( );
			pin = tokenBuilder.getPin( );
			accessToken = tokenBuilder.getAccessToken( );
			accessTokenSecret = tokenBuilder.getAccessTokenSecret( );
			urlValidation = tokenBuilder.getUrlAuth( );
		}
		txtConKey.setText( consumerKey );
		txtConKeySecret.setText( consumerKeySecret );
		txtPin.setText( pin );
		txtAccessToken.setText( accessToken );
		txtAccessTokenSecret.setText( accessTokenSecret );
		txtAuthUrl.setText( urlValidation );
	}

	void AtualizaStatus( ) {
		if ( ( ( ! ( this.txtConKey.getText( ).isEmpty( ) ) ) && ( ! ( this.txtConKeySecret.getText( ).isEmpty( ) ) ) ) && 
				( ! ( this.txtAccessToken.getText( ).isEmpty( ) ) ) && ( ! ( this.txtAccessTokenSecret.getText( ).isEmpty( ) ) ) ) {
			lblSituacao.setText("Acesso liberado.");
			lblSituacao.setForeground(Color.GREEN);
		} else if ( ( ( ! ( this.txtConKey.getText( ).isEmpty( ) ) ) && ( ! ( this.txtConKeySecret.getText( ).isEmpty( ) ) ) ) ) {
				lblSituacao.setText("Necessita buscar o PIN.");
				lblSituacao.setForeground(Color.RED);
				txtAuthUrl.setText( tokenBuilder.getUrlAuth( ) );
				txtAuthUrl.setEnabled( true );
		} else {
			lblSituacao.setText("Necessita configurar as chaves de acesso.");
			lblSituacao.setForeground(Color.RED);
		}
		lblSituacao.repaint( );
	}

	void BloqueiaJTextFields( ) {
		if ( txtConKey.getText( ).isEmpty( ) || txtConKeySecret.getText( ).isEmpty( ) ) {
			txtConKey.setEnabled( true );
			txtConKeySecret.setEnabled( true );
		}
		txtAccessToken.setEnabled( false );
		txtAccessTokenSecret.setEnabled( false );
		txtReqUrl.setEnabled( false );
		txtTokenUrl.setEnabled( false );
		txtAuthUrl.setEnabled( false );
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frmConfiguraAcessoA = new JFrame();
		frmConfiguraAcessoA.setResizable(false);
		frmConfiguraAcessoA.setSize(new Dimension(500, 500));
		frmConfiguraAcessoA.setTitle("Configura acesso a conta do Twitter");
		frmConfiguraAcessoA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmConfiguraAcessoA.getContentPane().setLayout(new BoxLayout(frmConfiguraAcessoA.getContentPane(), BoxLayout.Y_AXIS));
		
		JPanel panel_26 = new JPanel();
		panel_26.setPreferredSize(new Dimension(10, 250));
		frmConfiguraAcessoA.getContentPane().add(panel_26);
		panel_26.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_26.add(panel, BorderLayout.WEST);
		panel.setPreferredSize(new Dimension(150, 0));
		panel.setLayout(new GridLayout(17, 0, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		
		JLabel lblConsumerKey = new JLabel("Consumer Key:");
		lblConsumerKey.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConsumerKey.setHorizontalTextPosition(SwingConstants.LEADING);
		panel.add(lblConsumerKey);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		
		JLabel lblConsumerKeySecret = new JLabel("Consumer Key Secret:");
		lblConsumerKeySecret.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblConsumerKeySecret);
		
		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		
		JLabel lblNumeroPin = new JLabel("Numero PIN:");
		lblNumeroPin.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNumeroPin);
		
		JPanel panel_23 = new JPanel();
		panel.add(panel_23);
		
		JLabel lblAccessToken = new JLabel("Access Token:");
		lblAccessToken.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAccessToken.setHorizontalTextPosition(SwingConstants.LEADING);
		panel.add(lblAccessToken);
		
		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		
		JLabel lblAccessTokenSecret = new JLabel("Access Token Secret:");
		lblAccessTokenSecret.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAccessTokenSecret.setHorizontalTextPosition(SwingConstants.LEADING);
		panel.add(lblAccessTokenSecret);
		
		JPanel panel_13 = new JPanel();
		panel.add(panel_13);
		
		JLabel lblRequestTokenUrl = new JLabel("Request Token url:");
		lblRequestTokenUrl.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblRequestTokenUrl);
		
		JPanel panel_14 = new JPanel();
		panel.add(panel_14);
		
		JLabel lblAccessTokenUrl = new JLabel("Access Token url:");
		lblAccessTokenUrl.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblAccessTokenUrl);
		
		JPanel panel_15 = new JPanel();
		panel.add(panel_15);
		
		JLabel lblAuthorizeUrl = new JLabel("Authorize url:");
		lblAuthorizeUrl.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblAuthorizeUrl);
		
		JPanel panel_5 = new JPanel();
		panel_26.add(panel_5, BorderLayout.CENTER);
		panel_5.setPreferredSize(new Dimension(50, 10));
		
		JPanel panel_8 = new JPanel();
		panel_26.add(panel_8, BorderLayout.EAST);
		panel_8.setPreferredSize(new Dimension(320, 10));
		panel_8.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_7 = new JPanel();
		panel_7.setPreferredSize(new Dimension(100, 10));
		panel_8.add(panel_7, BorderLayout.EAST);
		
		JPanel panel_6 = new JPanel();
		panel_8.add(panel_6, BorderLayout.CENTER);
		panel_6.setLayout(new GridLayout(17, 0, 0, 0));
		
		JPanel panel_9 = new JPanel();
		panel_6.add(panel_9);
		
		txtConKey = new JTextField();
		panel_6.add(txtConKey);
		txtConKey.setColumns(10);
		
		JPanel panel_10 = new JPanel();
		panel_6.add(panel_10);
		
		txtConKeySecret = new JTextField();
		panel_6.add(txtConKeySecret);
		txtConKeySecret.setColumns(10);
		
		JPanel panel_11 = new JPanel();
		panel_6.add(panel_11);
		
		txtPin = new JTextField();
		panel_6.add(txtPin);
		txtPin.setColumns(10);
		
		JPanel panel_25 = new JPanel();
		panel_6.add(panel_25);
		
		txtAccessToken = new JTextField();
		panel_6.add(txtAccessToken);
		txtAccessToken.setColumns(10);
		
		JPanel panel_12 = new JPanel();
		panel_6.add(panel_12);
		
		txtAccessTokenSecret = new JTextField();
		panel_6.add(txtAccessTokenSecret);
		txtAccessTokenSecret.setColumns(10);
		
		JPanel panel_16 = new JPanel();
		panel_6.add(panel_16);
		
		txtReqUrl = new JTextField();
		panel_6.add(txtReqUrl);
		txtReqUrl.setColumns(10);
		
		JPanel panel_17 = new JPanel();
		panel_6.add(panel_17);
		
		txtTokenUrl = new JTextField();
		panel_6.add(txtTokenUrl);
		txtTokenUrl.setColumns(10);
		
		JPanel panel_18 = new JPanel();
		panel_6.add(panel_18);
		
		JPanel panel_28 = new JPanel();
		panel_6.add(panel_28);
		panel_28.setLayout(new BorderLayout(0, 0));
		
		txtAuthUrl = new JTextField();
		txtAuthUrl.setSize(new Dimension(80, 0));
		txtAuthUrl.setPreferredSize(new Dimension(80, 0));
		panel_28.add(txtAuthUrl, BorderLayout.CENTER);
		txtAuthUrl.setColumns(10);
		
		JButton buttonOpenIe = new JButton("GO");
		buttonOpenIe.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AbreNavegadorComUrl( );
			}
		});
		buttonOpenIe.setPreferredSize(new Dimension(45, 23));
		panel_28.add(buttonOpenIe, BorderLayout.EAST);
		
		JPanel panel_27 = new JPanel();
		panel_27.setSize(new Dimension(0, 100));
		panel_27.setPreferredSize(new Dimension(0, 50));
		frmConfiguraAcessoA.getContentPane().add(panel_27);
		panel_27.setLayout(new GridLayout(5, 2, 0, 0));
		
		JLabel lblSituaoDaConexo = new JLabel("SITUA\u00C7\u00C3O DA CONEX\u00C3O");
		lblSituaoDaConexo.setHorizontalAlignment(SwingConstants.CENTER);
		panel_27.add(lblSituaoDaConexo);
		
		lblSituacao = new JLabel("");
		panel_27.add(lblSituacao);
		
		JPanel panel_19 = new JPanel();
		panel_19.setSize(new Dimension(0, 50));
		frmConfiguraAcessoA.getContentPane().add(panel_19);
		panel_19.setPreferredSize(new Dimension(0, 50));
		panel_19.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_20 = new JPanel();
		panel_20.setPreferredSize(new Dimension(10, 60));
		panel_19.add(panel_20, BorderLayout.SOUTH);
		
		JPanel panel_21 = new JPanel();
		panel_19.add(panel_21, BorderLayout.CENTER);
		panel_21.setLayout(new GridLayout(0, 5, 0, 0));
		
		JPanel panel_22 = new JPanel();
		panel_21.add(panel_22);
		
		JButton btnRealizaAcessso = new JButton("ACESSA");
		btnRealizaAcessso.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				RealizaConexao( );
			}
		});
		panel_21.add(btnRealizaAcessso);
		
		JPanel panel_24 = new JPanel();
		panel_21.add(panel_24);
		
		JButton btnSaida = new JButton("SAIDA");
		panel_21.add(btnSaida);
	}

	void AbreNavegadorComUrl( ) {
		String camIePt = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
		String urlAut = txtAuthUrl.getText( );
		File ie = new File ( camIePt );
		if ( ie.exists( ) ) {
			try {
				Runtime.getRuntime().exec(camIePt + " " + urlAut);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	void RealizaConexao() {

		String consumerKey = "";
		String consumerKeySecret = "";
		String pin = "";

		if ( tokenBuilder != null ) {

			consumerKey = tokenBuilder.getConsumerKey();
			consumerKeySecret = tokenBuilder.getConsumerSecret();
			pin = tokenBuilder.getPin( );

			if ( ! ( txtConKey.getText( ).isEmpty( ) ) && ! ( txtConKeySecret.getText( ).isEmpty( ) ) ) {
				if ( ! ( consumerKey.equals( txtConKey.getText( ) ) ) || ! ( consumerKeySecret.equals( txtConKeySecret.getText( ) ) ) ) {
					consumerKey = txtConKey.getText( );
					consumerKeySecret = txtConKeySecret.getText( );
					tokenBuilder.setOAuthAccess( consumerKey , consumerKeySecret );
				}
			}

			if( ! ( pin.equals(txtPin.getText( ) ) ) ) {
				if ( ! ( txtPin.getText( ).isEmpty( ) ) ) {
					pin = txtPin.getText();
				}
			}
			
			tokenBuilder.setAccessToken( pin );

			tokenBuilder.setProperties("oauth.consumerKey",consumerKey);
			tokenBuilder.setProperties("oauth.consumerSecret",consumerKeySecret);
			/*tokenBuilder.setProperties("oauth.pin",pin);*/

			LoadVarToText( );
			AtualizaStatus( );

		}

	}

}